package com.xiaodou.mjf.web.console.basic.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.xiaodou.mjf.application.content.service.BannerService;
import com.xiaodou.mjf.application.core.service.FinancialPackageService;
import com.xiaodou.mjf.application.core.service.FinancingService;
import com.xiaodou.mjf.entity.content.Banner;
import com.xiaodou.mjf.entity.core.FinancialPackage;
import com.xiaodou.mjf.entity.core.Financing;
import com.xiaodou.mjf.infrastructure.common.controller.ApplicationController;
import com.xiaodou.mjf.infrastructure.common.utils.FileUtils;
import com.xiaodou.mjf.infrastructure.common.utils.Global;

/**
 * 广告管理Controller
 * 
 * @author BILLY
 *
 */
@Controller
@RequestMapping("advert")
public class AdvertController extends ApplicationController {

	@Resource
	BannerService bannerService;
	@Resource
	FinancingService financingService;
	@Resource
	FinancialPackageService financialPackageService;
	
	@RequestMapping("")
	public String banner(Banner banner) {

		return "content/banner/list";
	}

	@RequestMapping(value = { "data.json" })
	@ResponseBody
	public Map<String, Object> data(Banner banner) {
		Page<Banner> page = bannerService
				.pageAll(banner);
		return page(page);
	}

//	@RequestMapping(value = { "interestRate", "interestRate/{id}" })
//	@ResponseBody
//	public boolean checkInterestRate(@PathVariable String id, Integer term) {
//		boolean flag = false;
//		try {
//			Financing financing = new Financing();
//			financing = financingService.getFinancingByTermAndInvsetStatus(
//					term, Financing.INVEST_STATUS_SALE);
//			if (financing == null) {
//				flag = true;
//			}
//		} catch (Exception ex) {
//			logger.error(ex.toString());
//			throw ex;
//		}
//		return flag;
//	}

	@RequestMapping(value = { "form", "form/{id}" }, method = RequestMethod.GET)
	public String form(Model model, @PathVariable String id) {
		try {
			Banner banner = bannerService.get(id);
			model.addAttribute("banner", banner);
			// ftp ip address
			String imgFtpPath = Global.getImgPath();

			model.addAttribute("imgFtpPath", imgFtpPath);
			
			List<Financing> financinglist = financingService.getFinancingListByStatus(Financing.NORMAL);
			
			List<FinancialPackage> financialPackageList = financialPackageService.list();
			
			for (FinancialPackage financialPackage : financialPackageList) {
				Financing financing = new Financing();
				financing.setId(financialPackage.getId());
				financing.setTitle(financialPackage.getName());
				financinglist.add(financing);
			}
			
			model.addAttribute("lists", financinglist);
			
		} catch (Exception ex) {
			logger.error(ex.toString());
			throw ex;
		}

		return "content/banner/form";
	}
	
	@RequestMapping(value = { "add" }, method = RequestMethod.GET)
	public String add(Model model) {

		List<Financing> financing2 = financingService.getFinancingListByStatus(Financing.NORMAL);
		model.addAttribute("lists", financing2);
		List<Financing> financinglist = financingService.getFinancingListByStatus(Financing.NORMAL);
		List<FinancialPackage> financialPackageList = financialPackageService.list();
		
		for (FinancialPackage financialPackage : financialPackageList) {
			Financing financing = new Financing();
			financing.setId(financialPackage.getId());
			financing.setTitle(financialPackage.getName());
			financinglist.add(financing);
		}
		
		model.addAttribute("lists", financinglist);
		return "content/banner/add";
		
	}


	@RequestMapping(value = { "save" }, method = RequestMethod.POST)
	public String save(Banner banner, MultipartFile file, Model model,HttpServletRequest request)
			throws Exception {
		try {
			String path = FileUtils.getPath(Banner.IMAGE_PATH);
			if(file.getSize()>0) {
				String fileName = FileUtils.getFileName(file.getOriginalFilename());
				FileUtils.upload(Global.getFtpHost(), Global.getFtpUser(),
						Global.getFtpPassword(), Global.getFtpPort(),
						file.getInputStream(), path, fileName);
				banner.setImage(path + "/" + fileName);
			}
			bannerService.save(banner);

		} catch (Exception ex) {
			logger.error(ex.toString());
			ex.printStackTrace();
			throw ex;
		}
		return "redirect:";
	}

	@RequestMapping(value = { "delete", "delete/{id}" })
	@ResponseBody
	public Map<String, Object> delete(@PathVariable String id, Model model) {
		try {
			Banner banner = bannerService.get(id);
			if (banner != null) {
				banner.setStatus(Banner.DELETE);
				bannerService.save(banner);
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			throw ex;
		}
		return map(true);
	}
}
